package offer;

import binaryTree.basic.TreeNode;
import listNodePractice.ListNode;

import java.awt.geom.Area;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Offer32 {

    public int[] levelOrder(TreeNode root) {
        ArrayList<Integer> list = new ArrayList<>();
        Queue  queue = new LinkedList<TreeNode>();
        if (root!=null){
            queue.add(root);
        }
        while (!queue.isEmpty()){
            TreeNode cur = (TreeNode) queue.poll();
            list.add(cur.val);
            if (cur.left!=null){
                queue.offer(cur.left);

            }
            if (cur.right!=null){
                queue.offer(cur.right);
            }
        }
        int size = list.size();
        int[] res = new int[size];
        for (int i = 0; i < size; i++) {
            res[i]=list.get(i);
        }
        return res;
    }



    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode(1);
        TreeNode treeNode2 = new TreeNode(2);
        TreeNode treeNode3 = new TreeNode(3);
        TreeNode treeNode4 = new TreeNode(4);
        TreeNode treeNode5 = new TreeNode(5);
        treeNode1.left=treeNode2;
        treeNode1.right=treeNode3;
        treeNode2.left=treeNode4;
        treeNode2.right=treeNode5;


        Offer32 offer32 = new Offer32();
        int[] ints = offer32.levelOrder(treeNode1);
        for (int a :ints) {
            System.out.println(a);
        }


    }



}
